package com.ytjj.qmyx.mall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.constants.SysConfigConstants;
import com.ytjj.common.model.UsersResponse;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.SysConfigUtil;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.enums.ActivityTypeEnums;
import com.ytjj.qmyx.mall.mapper.*;
import com.ytjj.qmyx.mall.model.*;
import com.ytjj.qmyx.mall.model.dto.SignDto;
import com.ytjj.qmyx.mall.model.response.H5SignResponse;
import com.ytjj.qmyx.mall.model.response.ProductResponse;
import com.ytjj.qmyx.mall.model.response.ScoreHomeResponse;
import com.ytjj.qmyx.mall.service.BrandService;
import com.ytjj.qmyx.mall.utils.DateUtils;
import com.ytjj.qmyx.mall.utils.FakeUserUtil;
import com.ytjj.qmyx.mall.utils.PlatformDataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService {

    @Resource
    private RedisService redisService;

    @Resource
    private BrandMapper brandMapper;
    @Autowired
    private BrandService brandService;
    @Resource
    private FakeUserUtil fakeUserUtil;

    @Resource
    private ProductDao productDao;

    @Resource
    private ProductActivityMapper productActivityMapper;
    @Resource
    private ActivityColumnMapper activityColumnMapper;
    @Resource
    private SysConfigUtil sysConfigUtil;
    @Resource
    private ProductActivityDao productActivityDao;

    @Override
    public List<Brand> getBrandList() {
        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andStatusEqualTo(1);
        List<Brand> brandList = brandMapper.selectByExample(brandExample);
        return brandList.stream().sorted(Comparator.comparing(Brand::getSort)).collect(Collectors.toList());
    }

    @Resource
    private ProductMapper productMapper;

    @Override
    public List<ProductResponse> getBrandProductList(Integer brandId) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria()
                .andBankerIdEqualTo(brandId)
                .andStatusEqualTo((byte)1)
                .andPlatformDataIn(Arrays.asList("all", PlatformDataUtils.getPlatformData(TokenUtil.getSource())))
                .andActivityTypeEqualTo(ActivityTypeEnums.CASH_SCORE.name())
                .andIsAdvertEqualTo(0)
                .andIsHideEqualTo(0);
        List<Product> productList = productMapper.selectByExample(productExample);
        return productList.stream().map(ProductResponse::new)
                .collect(Collectors.toList());
    }

    /**
     * @Description 获取积分首页
     *
     * @param h5CurrentUser
     * @return
     */
    @Override
    public ScoreHomeResponse getScoreHome(UsersResponse h5CurrentUser) {
        ScoreHomeResponse scoreHomeResponse = new ScoreHomeResponse();
        // 品牌列表
        List<Brand> brandList = brandService.getBrandList();
        // 假用户列表
        List<UsersFake> fakeUserList = fakeUserUtil.getFakeUserList();
        // 积分猜你喜欢
        ActivityColumn guessLikeColumn = productDao.getProductActivityColumn("GET_LIKE");
        List<ProductResponse> guessLikeList = productDao.getProductListByColumnId(guessLikeColumn.getId());
        List<ProductResponse> guessLike = guessLikeList.stream()
                .limit(60)
                .map(item ->{
                    item.setRecImgFlag(0);
                    return item;
                })
                .collect(Collectors.toList());
        Collections.shuffle(guessLike);
        // 积分超值兑换
        ActivityColumn scoreChangeColumn = productDao.getProductActivityColumn("SUPER_CHANGE");
        List<ProductResponse> scoreChangeList = productDao.getProductListByColumnId(scoreChangeColumn.getId());
        // 免费兑换
        List<ProductResponse> freeProductList = productDao.getFreeProductList();
        scoreHomeResponse.setBrandList(brandList);
        scoreHomeResponse.setUsersFakeList(fakeUserList);
        scoreHomeResponse.setScoreChangeList(scoreChangeList.stream()
                .limit(20)
                .map(item ->{
                    item.setRecImgFlag(0);
                    return item;
                })
                .collect(Collectors.toList()));
        scoreHomeResponse.setGuessLikeList(guessLike);
        scoreHomeResponse.setFreeProductList(freeProductList.stream()
                .limit(20)
                .map(item ->{
                    item.setRecImgFlag(0);
                    return item;
                })
                .collect(Collectors.toList()));
        // 前端要的用户是否签到标志
        // 签到
        H5SignResponse h5SignResponse = new H5SignResponse();
        // 默认积分
        h5SignResponse.setExclusiveScore(Integer.valueOf(sysConfigUtil.getSysConfig(SysConfigConstants.DEFAULT_SCORE)));
        // 是否签到
        String signStr = redisService.get(com.ytjj.common.constants.RedisConstants.H5_SIGN +
                Optional.ofNullable(TokenUtil.getToken()).orElse("xxx"));
        SignDto signDto = null;
        if (signStr != null) {
            signDto = JSONObject.parseObject(signStr, SignDto.class);
            try {
                int isYeaterday = DateUtils.isYeaterday(signDto.getSignTime(), new Date());
                if (isYeaterday == -1) {
                    h5SignResponse.setIsSign(1);
                } else if (isYeaterday == 1) {
                    h5SignResponse.setIsSign(0);
                } else {
                    h5SignResponse.setIsSign(0);
                    redisService.remove(com.ytjj.common.constants.RedisConstants.H5_SIGN + TokenUtil.getToken());
                    signDto = null;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            h5SignResponse.setIsSign(0);
        }
        h5SignResponse.setTomorrowScore(Integer.valueOf(sysConfigUtil.getSysConfig(SysConfigConstants.H5_SIGN_SCORE)));
        scoreHomeResponse.setIsSign(h5SignResponse.getIsSign());
        return scoreHomeResponse;
    }
}
